#!/bin/bash
set -e

login_user=`/usr/bin/stat -f%Su /dev/console`
squirrel_app_root="${DSTROOT}/Squirrel.app"
squirrel_executable="${squirrel_app_root}/Contents/MacOS/Squirrel"
rime_package_installer="${squirrel_app_root}/Contents/MacOS/rime-install"
rime_shared_data_path="${squirrel_app_root}/Contents/SharedSupport"

/usr/bin/sudo -u "${login_user}" /usr/bin/killall Squirrel > /dev/null || true

"${squirrel_executable}" --register-input-source

if [ -z "${RIME_NO_PREBUILD}" ]; then
    pushd "${rime_shared_data_path}" > /dev/null
    "${squirrel_executable}" --build
    popd > /dev/null
fi && (
    /usr/bin/sudo -u "${login_user}" "${squirrel_executable}" --enable-input-source
    /usr/bin/sudo -u "${login_user}" "${squirrel_executable}" --select-input-source
)
